package com.next.aappublicapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.next.aap.client.CandidateSelectionProcess;
import com.next.aappublicapp.server.service.DataServices;
import com.next.aappublicapp.tasks.AapAsyncTask;
import com.next.aappublicapp.util.PreferencesUtil;
import com.nostra13.universalimageloader.core.ImageLoader;

public class CandidateSelectionProcessActivity extends Activity {

	public static final String ARGS_BUNDLE = "ARGS_BUNDLE";
	public static final String ARGS_IS_ENGLISH = "ARGS_IS_ENGLISH";
	private ImageView cspImageView;
	private TextView cspContent;
	private LinearLayout loadingStatus;
	private LinearLayout cspLinearLayout;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_candidate_selection_process);
		
		cspImageView = (ImageView)findViewById(R.id.cspImage);
		cspContent = (TextView)findViewById(R.id.cspContent);
		loadingStatus = (LinearLayout)findViewById(R.id.loading_status);
		cspLinearLayout = (LinearLayout)findViewById(R.id.cspLinearLayout);
		Bundle bundle = getIntent().getBundleExtra(ARGS_BUNDLE);
		boolean isEnglish = bundle.getBoolean(ARGS_IS_ENGLISH);        

		CandidateSelectionProcess candidateSelectionProcess = PreferencesUtil.getInstance().getCandidateSelectionProcessCache(this, isEnglish);
		loadCandidateSelectionProcessAsync(isEnglish);
		/*
		if(candidateSelectionProcess != null && candidateSelectionProcess.getMessage()!= null && candidateSelectionProcess.getMessage().length() > 200){
			loadCandidateSelectionProcess(candidateSelectionProcess);
		}else{
			loadCandidateSelectionProcessAsync(isEnglish);
		}
		*/
	}

	private void loadCandidateSelectionProcess(CandidateSelectionProcess candidateSelectionProcess){
		if(candidateSelectionProcess != null){
			if(candidateSelectionProcess.getImageUrl() != null){
				ImageLoader.getInstance().displayImage(candidateSelectionProcess.getImageUrl(), cspImageView);
			}
			if(candidateSelectionProcess.getMessage() != null){
				cspContent.setText(candidateSelectionProcess.getMessage());
			}
		}else{
			cspContent.setText("Content can not be loaded at the moment. Please try again later");
		}
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.candidate_selection_process, menu);
		return true;
	}
	
	private void loadCandidateSelectionProcessAsync(final boolean isEnglish){
		String label = "English";
		if(!isEnglish){
			label = "Hindi";
		}
		new AapAsyncTask<String, String, CandidateSelectionProcess>("CandidateSelectionProcess",label) {
			@Override
			protected void beforeExecute(){
				loadingStatus.setVisibility(View.VISIBLE);
				cspLinearLayout.setVisibility(View.GONE);
			}
			@Override
			protected CandidateSelectionProcess doInBackground(String... params) {
				CandidateSelectionProcess candidateSelectionProcess = null;
				try{
					if(isEnglish){
						candidateSelectionProcess = DataServices.getInstance().getEnglishCandidateSelectionProcess(CandidateSelectionProcessActivity.this);	
					}else{
						candidateSelectionProcess = DataServices.getInstance().getHindiCandidateSelectionProcess(CandidateSelectionProcessActivity.this);
					}
					
				}catch(Exception ex){
					
				}
				return candidateSelectionProcess;
			}
			@Override
			protected void afterExecute(CandidateSelectionProcess candidateSelectionProcess){
				loadingStatus.setVisibility(View.GONE);
				cspLinearLayout.setVisibility(View.VISIBLE);
				loadCandidateSelectionProcess(candidateSelectionProcess);
			}
			
		}.execute();
	}

}

